package com.wl.boot.batch.controller;

import com.wl.boot.batch.exception.ExcelException;
import com.wl.boot.batch.service.ExcelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/excel")
public class excelController {
    private static final Logger logger = LoggerFactory.getLogger(excelController.class);

    @Autowired
    private ExcelService excelService;

    @PostMapping("/upload")
    public String upload(MultipartFile file,
                         @RequestParam("excelId") String excelId) throws Exception {
        String filename = file.getOriginalFilename();
        if (filename == null || (!filename.toLowerCase().endsWith(".xls") && !filename.toLowerCase().endsWith(".xlsx"))) {
            throw new ExcelException("文件格式错误！");
        }
        excelService.parserExcel(file, excelId);
        return "success";
    }

    @PostMapping("/uploads")
    public String upload(MultipartFile[] file,
                         @RequestParam("excelId") String excelId) throws Exception {
        excelService.parserExcels(file, excelId);
        return "success";
    }

    @PostMapping("/uploadEEC")
    public String uploadEEC(MultipartFile file,
                         @RequestParam("excelId") String excelId) throws Exception {
        String filename = file.getOriginalFilename();
        if (filename == null || (!filename.toLowerCase().endsWith(".xls") && !filename.toLowerCase().endsWith(".xlsx"))) {
            throw new ExcelException("文件格式错误！");
        }
        excelService.parserExcelEEC(file, excelId);
        return "success";
    }
}
